﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using static System.Collections.Specialized.BitVector32;

namespace GXWA_DATA.Config
{
    public static class MappingTable
    {
        /// <summary>
        /// 工位号与状态偏移量映射
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, int> GetStation()
        {
            Dictionary<string, int> stations = new Dictionary<string, int>()
            {
                {"Op10A", 0},
                {"Op10B", 1},
                {"Op20A", 2},
                {"Op20B", 3},
                {"Op30A", 4},
                {"Op30B", 5},
                {"Op40A", 6},
                {"Op40B", 7},
                {"Op45A", 8},
                {"Op45B", 9},
                {"Op50A", 10},
                {"Op50B", 11},
                {"Op60A", 12},
                {"Op60B", 13},
                {"OK", 13},
                {"NG", 13},
            };

            return stations;
        }

        /// <summary>
        /// 工位号与PLC映射
        /// </summary>
        /// <param name="station"></param>
        /// <returns></returns>
        public static string GetPlcNameByStation(string station)
        {
            Dictionary<string, string> plcNames = new Dictionary<string, string>()
            {
                {"Op10A", "Weld1"},
                {"Op10B", "Weld1"},
                {"Op20A", "Weld2"},
                {"Op20B", "Weld2"},
                {"Op30A", "Weld3"},
                {"Op30B", "Weld3"},
                {"Op40A", "Weld4"},
                {"Op40B", "Weld4"},
                {"Op45A", "LaserCutter"},
                {"Op45B", "LaserCutter"},
                {"Op50A", "Weld5"},
                {"Op50B", "Weld5"},
                {"Op60A", ""},
                {"Op60B", ""},
            };

            if (plcNames.TryGetValue(station, out string value))
                return value;

            return "";
        }

        /// <summary>
        /// PLC与关键数据偏移量映射
        /// </summary>
        /// <param name="plcName"></param>
        /// <returns></returns>
        public static Dictionary<string, int> GetDataOffByStation(string plcName)
        {
            Dictionary<string, Dictionary<string, int>> dataOff = new Dictionary<string, Dictionary<string, int>>();

            dataOff["Weld1"] = new Dictionary<string, int>()
            {
                { "Code_A", 1268},
                { "Code_B", 2792},

                { "WeldPoint_A", 2792},
                { "WeldPoint_B", 2792},

                { "WeldI_A", 2792},
                { "WeldI_B", 2792},

                { "WeldU_A", 2792},
                { "WeldU_B", 2792},

                { "WeldSpeed_A", 2792},
                { "WeldSpeed_B", 2792},

                { "WeldStatus_A", 2792},
                { "WeldStatus_B", 2792},
            };

            dataOff["Weld2"] = new Dictionary<string, int>()
            {
                { "Code_A", 2792},
                { "Code_B", 2792},

                { "WeldPoint_A", 2792},
                { "WeldPoint_B", 2792},

                { "WeldI_A", 2792},
                { "WeldI_B", 2792},

                { "WeldU_A", 2792},
                { "WeldU_B", 2792},

                { "WeldSpeed_A", 2792},
                { "WeldSpeed_B", 2792},

                { "WeldStatus_A", 2792},
                { "WeldStatus_B", 2792},
            };

            dataOff["Weld3"] = new Dictionary<string, int>()
            {
                { "Code_A", 2792},
                { "Code_B", 2792},

                { "WeldPoint_A", 2792},
                { "WeldPoint_B", 2792},

                { "WeldI_A", 2792},
                { "WeldI_B", 2792},

                { "WeldU_A", 2792},
                { "WeldU_B", 2792},

                { "WeldSpeed_A", 2792},
                { "WeldSpeed_B", 2792},

                { "WeldStatus_A", 2792},
                { "WeldStatus_B", 2792},
            };

            dataOff["Weld4"] = new Dictionary<string, int>()
            {
                { "Code_A", 2792},
                { "Code_B", 2792},

                { "WeldPoint_A", 2792},
                { "WeldPoint_B", 2792},

                { "WeldI_A", 2792},
                { "WeldI_B", 2792},

                { "WeldU_A", 2792},
                { "WeldU_B", 2792},

                { "WeldSpeed_A", 2792},
                { "WeldSpeed_B", 2792},

                { "WeldStatus_A", 2792},
                { "WeldStatus_B", 2792},
            };

            dataOff["LaserCutter"] = new Dictionary<string, int>()
            {

            };

            dataOff["Weld5"] = new Dictionary<string, int>()
            {
                { "Code_A", 2792},
                { "Code_B", 2792},

                { "WeldPoint_A", 2792},
                { "WeldPoint_B", 2792},

                { "WeldI_A", 2792},
                { "WeldI_B", 2792},

                { "WeldU_A", 2792},
                { "WeldU_B", 2792},

                { "WeldSpeed_A", 2792},
                { "WeldSpeed_B", 2792},

                { "WeldStatus_A", 2792},
                { "WeldStatus_B", 2792},
            };

            if (dataOff.TryGetValue(plcName, out var value))
                return value;

            return null;
        }

        /// <summary>
        /// 获取Cpu唯一标识
        /// </summary>
        public static string DeviceIdentifier()
        {
            string cpuId = string.Empty;
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select ProcessorId from Win32_Processor"))
            {
                foreach (ManagementObject obj in searcher.Get())
                {
                    cpuId = obj["ProcessorId"].ToString();
                    break;
                }
            }
            return cpuId;
        }

        internal static string GetDataBasePasswordByCpuId(string cpuId)
        {
            return cpuId.Equals("178BFBFF00A70F52") ? "yk19991223" : "dh@admin";
        }
    }
}
